﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Threading;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.FileMaintenance;
using BrainTechLLC.WPF;
using DragDropLibrary;

using System.ComponentModel;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.FolderExplorer
{
    /// <summary>
    /// Interaction logic for FolderExplorerControl.xaml
    /// </summary>
    public partial class FolderExplorerControl : UserControl
    {
        protected TreeViewItem _lastSelected;
        protected ThreadSafeList<TreeViewItem> _TreeViewItems = new ThreadSafeList<TreeViewItem>();
        public ThreadSafeLookupNonRef<FolderViewModel, bool> _Selected = new ThreadSafeLookupNonRef<FolderViewModel, bool>();

        private void tvRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem tvi = BrainTechLLC.WPF.TempExten.FindParent<TreeViewItem>(sender as FrameworkElement);
            FolderViewModel f = tvi.Tag as FolderViewModel;

            if (f == null)
                return;

            ChangeSelectedState(tvi, !f.IsSelected, false);            
        }

        private void tvRoot_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem tvi = e.OriginalSource as TreeViewItem;
            tvi.IsSelected = false;
        }

        void ChangeSelectedState(TreeViewItem treeViewItem, bool selected, bool noMulti)
        {
            FolderViewModel folder = treeViewItem.Tag as FolderViewModel;

            if (!noMulti)
            {
                treeViewItem.IsSelected = false;
                // treeViewItem.Focus();

                if (_shiftDown && _lastSelected != null)
                {
                    FolderViewModel f = _lastSelected.Tag as FolderViewModel;
                    //TreeView tv = _lastSelected.FindParent<TreeView>();
                    //var tvi = tv.GetActualChildren<TreeViewItem>();
                    int indexLast = -1;
                    for (int n = 0; n < this._TreeViewItems.Count; n++)
                    {
                        TreeViewItem item = this._TreeViewItems[n];
                        if (item == _lastSelected)
                        {
                            indexLast = n;
                        }
                        if (item == treeViewItem)
                        {
                            if (indexLast >= 0)
                            {
                                for (int i = indexLast + 1; i < n; i++)
                                {
                                    TreeViewItem itemSelect = this._TreeViewItems[i];
                                    ChangeSelectedState(itemSelect, selected, true);
                                }
                            }
                            else
                            {
                                for (int i = n + 1; i < this._TreeViewItems.Count; i++)
                                {
                                    TreeViewItem itemSelect = this._TreeViewItems[i];
                                    if (itemSelect == _lastSelected)
                                    {
                                        break;
                                    }
                                    ChangeSelectedState(itemSelect, selected, true);
                                }
                            }
                        }
                    }
                }
                else if (_controlDown)
                {
                }
                else if (!_controlDown && !_shiftDown)
                {
                    _Selected.AllKeyValuePairs.ForEach(kvp=>kvp.Key.IsSelected = false);
                    _Selected.Clear();
                    //RootFolder.GetAllFoldersInMemory().ForEach(f => f.IsSelected = false);
                }
            }

            if (!selected)
            {                
                folder.IsSelected = false;
                _Selected.Remove(folder);
            }
            else
            {
                folder.IsSelected = true;
                _Selected.Add(folder, true);
            }

            _lastSelected = treeViewItem;
        }
    }
}